package com.tianpengtech.admin.moneylog;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.admin.dictionary.DictionaryDao;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.dictionary.Dictionary;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.moneylog.MoneyLog;
import com.tianpengtech.common.util.JsonUtil;

/**
* 路由配置地址：add("/admin/moneylog", MoneyLogAdminController.class, "/admin/moneylog");
* 数据库映射地址：arp.addMapping(MoneyLog.dao.tableName(),MoneyLog.class);
*/
@Before(InitInterceptor.class)
public class MoneyLogAdminController extends AbstractAdminController{

	@Override
	protected void config() {
		this.clazz=MoneyLog.class;
		this.service=new MoneyLogAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	@Override
	public void parsePageRecord(Object results) {
		
		MoneyLog moneyLog = (MoneyLog)results;
		
		long wh_id = moneyLog.getLong(MoneyLog.WH_ID);
		long oper_id = moneyLog.getLong(MoneyLog.OPERID);
		moneyLog.put("type",moneyLog.type());
		moneyLog.set(MoneyLog.DELETETAG,moneyLog.status());
		Fair temp =  Fair.dao.findById(wh_id);
		moneyLog.put("wh_str",temp.getStr(Fair.NICK_NAME));
		temp =  Fair.dao.findById(oper_id);
		moneyLog.put("oper_str",temp.getStr(Fair.NICK_NAME));
	}
	
	
	public void accept(){
		int wh_id = getParaToInt("wh_id");
		int id = getParaToInt("id");
		MoneyLog moneyLog = MoneyLog.dao.findById(id);
		if(moneyLog!=null){
			if(moneyLog.getInt(MoneyLog.DELETETAG)!=MoneyLog.STATUS_APPROVING){
				renderJson(JsonUtil.printError("当前记录已经被处理了，请勿重复操作"));
				return;
			}
			Dictionary dict = DictionaryDao.ME.readDirecotryByCode(300100);//邀请金额
			String val = dict.get(Dictionary.NAME, 5);
			Db.update("update fair_wh set money=money+"+val+" where id = ?",wh_id);
			moneyLog.set(MoneyLog.DELETETAG, MoneyLog.STATUS_SUCCESS).update();
			renderJson(JsonUtil.printSuccess("操作成功"));
		}else{
			renderJson(JsonUtil.printError("操作成功"));
		}
	}
	
	public void unaccept(){
		int id = getParaToInt("id");
		MoneyLog moneyLog = MoneyLog.dao.findById(id);
		if(moneyLog!=null){
			if(moneyLog.getInt(MoneyLog.DELETETAG)!=MoneyLog.STATUS_APPROVING){
				renderJson(JsonUtil.printError("当前记录已经被处理了，请勿重复操作"));
				return;
			}
			moneyLog.set(MoneyLog.DELETETAG, MoneyLog.STATUS_ERROR).update();
			renderJson(JsonUtil.printSuccess("操作成功"));
		}else{
			renderJson(JsonUtil.printError("操作成功"));
		}
	}
}

	